import time

from selenium.webdriver.common.by import By
from common.common import driver

# 私信测试
class TestMessage:
    url=""
    driver=""

    def __init__(self):
        self.driver=driver.driver
        self.url="http://127.0.0.1:58080/index.html"
        self.driver.get(self.url)

    # 成功发送私信
    def Sucsend_message(self):
        # 选择他人帖子
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(2) > div > div.col > div.text-truncate > a").click()
        # 在帖子详情页点击发私信按钮
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR,"#btn_details_send_message").click()
        # 出现弹窗
        time.sleep(0.5)
        text=self.driver.find_element(By.CSS_SELECTOR,"#index_message_modal > div > div > div.modal-header > h5").text
        assert text=="发送站内信"
        # 添加屏幕截图
        driver.getScreeShot()
        # 在他人主页点击发送私信
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_receive_cancel").click()
        self.driver.find_element(By.CSS_SELECTOR,"#article_details_author_name").click()
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR,"#btn_profile_send_message").click()
        # 出现弹窗
        time.sleep(0.5)
        text = self.driver.find_element(By.CSS_SELECTOR,"#index_message_modal > div > div > div.modal-header > h5").text
        assert text == "发送站内信"
        # 找到输入框即弹窗出现，输入内容，点击发送
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_receive_content").send_keys("1")
        self.driver.find_element(By.CSS_SELECTOR,"#btn_index_send_message").click()
        # 出现提示发送成功弹窗即发送成功
        time.sleep(0.5)
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text == "成功"
        # 添加屏幕截图
        driver.getScreeShot()
        # 返回主页
        self.driver.find_element(By.CSS_SELECTOR,"#nav_board_index > a").click()

    # 失败发送私信
    def Failsend_message(self):
        # 选择他人帖子
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(2) > div > div.col > div.text-truncate > a").click()
        # 在帖子详情页点击发私信按钮
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "#btn_details_send_message").click()
        # 出现弹窗，找到输入框即弹窗出现，不输入内容，点击发送
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_receive_content")
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_send_message").click()
        # 出现提示警告弹窗即发送失败
        time.sleep(0.5)
        text=self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text=="警告"
        # 添加屏幕截图
        driver.getScreeShot()
        # 返回主页
        self.driver.find_element(By.CSS_SELECTOR, "#nav_board_index > a").click()

    # 成功回复私信
    def Sucreply_message(self):
        # 点击消息提示
        self.driver.find_element(By.CSS_SELECTOR, "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg").click()
        # 弹出所有消息列表，若找到所有站内信即弹出列表
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "#offcanvasEndLabel")
        # 是否出现新消息
        self.driver.find_element(By.CSS_SELECTOR, "#index_div_message_list > div:nth-child(1)")
        # 消息状态是否为未读
        text=self.driver.find_element(By.CSS_SELECTOR, "#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a > span.index_message_item_statue").text
        assert text=="[未读]"
        # 添加屏幕截图
        driver.getScreeShot()
        # 点击消息
        self.driver.find_element(By.CSS_SELECTOR, "#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a").click()
        # 出现回复弹窗，点击取消
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_reply_cancel").click()
        # 消息状态变为已读
        text = self.driver.find_element(By.CSS_SELECTOR,"#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a > span.index_message_item_statue").text
        assert text == "[已读]"
        # 添加屏幕截图
        driver.getScreeShot()
        # 点击消息
        self.driver.find_element(By.CSS_SELECTOR,"#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a").click()
        # 出现回复弹窗，点击回复按键
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_message_reply").click()
        # 输入内容，点击发送
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_reply_receive_content").send_keys("1")
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_send_message_reply").click()
        # 提示成功即回复成功
        time.sleep(0.5)
        text=self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text=="成功"
        # 添加屏幕截图
        driver.getScreeShot()
        # 消息状态变为已回复
        text=self.driver.find_element(By.CSS_SELECTOR, "#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a > span.index_message_item_statue").text
        assert text=="[已回复]"
        # 添加屏幕截图
        driver.getScreeShot()
        # 返回主页
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_offcanvasEnd > div.offcanvas-header > button").click()

    def Failreply_message(self):
        # 点击消息提示
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg").click()
        # 弹出所有消息列表，若找到所有站内信即弹出列表
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "#offcanvasEndLabel")
        # 直接点击消息后面的回复按钮
        self.driver.find_element(By.CSS_SELECTOR, "#index_div_message_list > div:nth-child(1) > div > div:nth-child(3) > a > svg").click()
        # 出现回复弹窗，点击回复按键
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_message_reply").click()
        # 不输入内容，点击发送
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_reply_receive_content")
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_send_message_reply").click()
        # 出现警告为回复失败
        time.sleep(0.5)
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text=="警告"
        # 添加屏幕截图
        driver.getScreeShot()
        # 返回主页
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_offcanvasEnd > div.offcanvas-header > button").click()

